//
//  FourViewController.m
//  HGY
//
//  Created by lanou3g on 15/10/27.
//  Copyright (c) 2015年 顾成辉. All rights reserved.
//

#import "FourViewController.h"
#import "my1ViewController.h"
#import "sqManager.h"
#import "sqFenLei.h"
#import "thirdSQManager.h"

@interface FourViewController ()<UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, retain) NSMutableArray *firstDataArray;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UIButton *landingbutton;
@property (nonatomic,retain) UIImageView *imageView;





@end

@implementation FourViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
//    [self stuUpData];
    [self addTableView];
    
 

    
    

}



- (void)addTableView
{
   

   
    
   
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];

    
   
    

    
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 150 * kHeight)];
    headerView.backgroundColor = [UIColor whiteColor];
    
    

    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(kScreenWidth / 2 - 30 * kWidth, 25 * kHeight, kScreenWidth / 5, kScreenWidth / 5);
    
    
    UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth / 2 - 30 * kWidth, 25 * kHeight, kScreenWidth / 5, kScreenWidth / 5)];
    imageview.image = [UIImage imageNamed:@"P14H6QV5W(`1KD[[74`{BIC.jpg"];
    
    // 弧度
    imageview.layer.cornerRadius = 40 * kHeight;
    imageview.layer.masksToBounds = YES;
    
    [headerView addSubview:imageview];
    
    
    
    // 弧度
    self.button.layer.cornerRadius = 40 * kHeight;



    
    [self.button addTarget:self action:@selector(actionPlayOrPause:) forControlEvents:UIControlEventTouchUpInside];
    
    [headerView addSubview:self.button];
    
    
    self.landingbutton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.landingbutton.frame = CGRectMake(self.button.left + 10 *kWidth, self.button.top + 90 *kHeight, 60 *kWidth, 20 *kHeight);
    
//    self.landingbutton.backgroundColor = [UIColor blackColor];
    [self.landingbutton setTitle:@"登陆" forState:(UIControlStateNormal)];
    [self.landingbutton setTitle:@"登陆" forState:(UIControlStateHighlighted)];

    
    [self.landingbutton setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
    [self.landingbutton setTitleColor:[UIColor whiteColor] forState:(UIControlStateHighlighted)];
    

    
    
    
    [self.landingbutton addTarget:self action:@selector(actionlanding:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:self.landingbutton];

//    UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//    imageView.image = [UIImage imageNamed:@"P14H6QV5W(`1KD[[74`{BIC.jpg"];
    
//    // 透明度
//    imageView.alpha = 0.6;
//    [headerView addSubview:imageView];
    
 
    self.tableView.tableHeaderView = headerView;
 
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    
    
    [self.view addSubview:self.tableView];
    
    
    
    
}


- (void)actionlanding:(UIButton *)button
{
    
}



- (void)actionPlayOrPause:(UIButton *)button
{
    
    // 点击更换相册里图片
    UIImagePickerController  *ipC=[[UIImagePickerController alloc] init];
    //UIImagePickerControllerSourceTypePhotoLibrary,  全部图片
    //UIImagePickerControllerSourceTypeCamera, 相机照相取图片
    //UIImagePickerControllerSourceTypeSavedPhotosAlbum  只是存储的图片
    ipC.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    ipC.delegate = self;
    //和入栈类似
    [self presentViewController:ipC animated:YES completion:nil];
}


// 点击更换相册里图片
//选择
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    
    [self.button setImage:image forState:UIControlStateNormal];
    // 设置为圆形
    self.button.layer.masksToBounds = YES;
    //拿开
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}



//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return 1;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}





- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
   
        NSString *identifier = @"MyCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
            cell.backgroundColor = [UIColor clearColor];
            
        }

    
                // 设置字体
        //    cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-bold" size:22];
        //    cell.textLabel.textColor = [UIColor whiteColor];
        //    cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
        //    cell.selectedBackgroundView = [[UIView alloc] init];
        
        
        
            NSArray *titles = @[@"美食收藏", @"分类收藏",@"视频收藏", @"清理缓存", @"关于我们", @"当前版本",@"夜间模式"];
            NSArray *images = @[@"food",@"Fruits", @"Video",@"3.jpg",@"5.jpg", @"6.jpg", @"1.jpg",@"22.jpg"];
        
            cell.textLabel.text = titles[indexPath.row];
            cell.imageView.image = [UIImage imageNamed:images[indexPath.row]];
        
        
            if (indexPath.row == 6) {
                UISwitch *nightSwitch=[[UISwitch alloc]initWithFrame:CGRectMake(300 *kWidth,10 * kHeight, 30*kWidth, 40 * kHeight)];
                [nightSwitch addTarget:self action:@selector(night:) forControlEvents:UIControlEventValueChanged];
                [nightSwitch setOn:NO animated:YES];
                [cell addSubview:nightSwitch];
            }
        
        
            
            return cell;
     
    
//    NSString *identifier = @"MyCell";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//    if (cell == nil) {
//        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
//        cell.backgroundColor = [UIColor clearColor];
//        
//        
//    }
//    
//    // 设置字体
////    cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-bold" size:22];
////    cell.textLabel.textColor = [UIColor whiteColor];
////    cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
////    cell.selectedBackgroundView = [[UIView alloc] init];
//
//
//
//    NSArray *titles = @[@"我的收藏", @"清理缓存", @"关于我们", @"当前版本",@"夜间模式"];
//    NSArray *images = @[@"3.jpg",@"5.jpg", @"6.jpg", @"1.jpg",@"22.jpg"];
//    
//    cell.textLabel.text = titles[indexPath.row];
//    cell.imageView.image = [UIImage imageNamed:images[indexPath.row]];
//    
//    
//    if (indexPath.row == 4) {
//        UISwitch *nightSwitch=[[UISwitch alloc]initWithFrame:CGRectMake(300 *kWidth,10 * kHeight, 30*kWidth, 40 * kHeight)];
//        [nightSwitch addTarget:self action:@selector(night:) forControlEvents:UIControlEventValueChanged];
//        [nightSwitch setOn:NO animated:YES];
//        [cell addSubview:nightSwitch];
//    }
//
//
//    
//    return cell;
}
// 夜间模式
-(void)night:(UISwitch *)swit
    {
        if ([swit isOn]) {
            self.view.window.alpha=0.5;
        }
        else{
            self.view.window.alpha=1;
        }
    }
    

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0) {
        
      //  NSMutableArray *arr1 = [[sqManager shareManager] queryAll];
        my1ViewController *vc = [[my1ViewController alloc] init];
//        vc.dataArray = arr1;
        vc.index = indexPath.row;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (indexPath.row == 1) {
       // NSMutableArray *arr2 = [[sqFenLei shareManager] queryAll];
        my1ViewController *vc = [[my1ViewController alloc] init];
//        vc.dataArray = arr2;
        vc.index = indexPath.row;
//
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (indexPath.row == 2) {
       //  NSMutableArray *arr3 = [[thirdSQManager shareManager]queryAll];
        my1ViewController *vc = [[my1ViewController alloc] init];
//        vc.dataArray = arr3;
        vc.index = indexPath.row;

        [self.navigationController pushViewController:vc animated:YES];
    }
    
    
    
   
    
  
    if(indexPath.row == 3)
    {
        NSString * cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
        float HCSize = [self folderSizeAtPath:cachPath];
        NSString *str=[NSString stringWithFormat:@"\n 当前缓存大小:%.1fM,是否确定清理缓存?", HCSize];
        UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
        [alertView show];
    }
    
    if(indexPath.row == 4)
    {
        UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"关于我们" message:@"\n本应用所有内容收集整理均来自互联网,访问者可将本网站内容用于学习欣赏,以及其他非商业用途,并同时遵循本网站合法权利。" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alertView show];

    }
    
    if (indexPath.row == 5) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"当前版本" message:@"版本V1.0.0" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
    
   

}







//- (void)p_removeAlertView:(UIAlertView *)alertView
//{
//    //alertView消失
//    [alertView dismissWithClickedButtonIndex:0 animated:YES];
//
//}

#pragma mark - 计算缓存大小
//获取文件夹下的所有文件大小
- (float ) folderSizeAtPath:(NSString*) folderPath
{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString *fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}
//单个文件的大小
- (float) fileSizeAtPath:(NSString*) filePath
{
    NSFileManager* manager = [NSFileManager defaultManager];
    
    if ([manager fileExistsAtPath:filePath]){
        
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    
    return 0;
}

//alertview点击
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self clear];
    }
}


/**清除缓存 */
- (void)clear
{
    dispatch_async(
        dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString * cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
        NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                       
                       
        for (NSString *p in files) {
            NSError *error;
            NSString *path = [cachPath stringByAppendingPathComponent:p];
            if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                
            
                }
            }
                       
        });
    
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
